package main

/*八皇后问题*/
import "fmt"

const (
	WEI = 4
	DANGER=9
)

var Count = 0

func shang(r, c int, chess [][WEI]int)  {
	for i:=0;i<WEI;i++{
		if(chess[r][i]!=1){
			chess[r][i]=DANGER
		}
		if(chess[i][c]!=1){
			chess[i][c]=DANGER
		}
	}

	//左上角
	for  i,j:=r-1,c-1; i>=0 && j>=0; i,j=i-1,j-1{
		if(chess[i][j]!=1){
			chess[i][j]=DANGER
		}
	}

	//左下角
	for  i,j:=r+1,c-1; i<WEI && j>=0; i,j=i+1,j-1{
		if(chess[i][j]!=1){
			chess[i][j]=DANGER
		}
	}

	//右上角
	for  i,j:=r-1,c+1; i>=0 && j<WEI; i,j=i-1,j+1{
		if(chess[i][j]!=1){
			chess[i][j]=DANGER
		}
	}

	//右下角
	for  i,j:=r+1,c+1; i<WEI && j<WEI; i,j=i+1,j+1{
		if(chess[i][j]!=1){
			chess[i][j]=DANGER
		}
	}

}

func eightQueen(r, c int, chess [][WEI]int) {
	tmp :=  make([][WEI]int, WEI)
	var i, j int
	for i = 0; i < WEI; i++ {
		for j = 0; j < WEI; j++ {
			if(r==0){
				tmp[i][j] = 0;
			}else {
				tmp[i][j] = chess[i][j]
			}
		}
	}

	if (r == WEI) {
		fmt.Printf("第%d种解法\n ",Count+1);
		for i = 0; i < WEI; i++ {
			for j = 0; j < WEI; j++ {
				fmt.Printf("%d ", tmp[i][j]);
			}
			fmt.Printf("\n ");
		}
		fmt.Printf("\n ");
		Count=Count+1
	}else {

		for j = 0; j < c; j++ {
                       print(r,j,tmp)
			fmt.Printf("\n ");
			if(tmp[r][j]==0){ //判断是否危险

				fmt.Printf("%d,%d,没有危险!\n",r,j)

				tmp[r][j]=1
				shang(r,j,tmp)

				eightQueen(r+1,c,tmp);
			}
		}

	}
}

func print(r,j int, tmp [][WEI]int)  {
	fmt.Printf("尝试 %d,%d,\n",r,j)
	for i  := 0; i < WEI; i++ {
		for j := 0; j < WEI; j++ {
			fmt.Printf("%d ", tmp[i][j]);
		}
		fmt.Printf("\n ");
	}
}
func main() {
	a := make([][WEI]int, WEI)

	fmt.Printf("%v\n", a)

	eightQueen(0, WEI, a)


}
